package com.laowang.project.system.service.impl;

import java.util.List;

import com.laowang.common.utils.MobileCheck;
import com.laowang.project.system.domain.MyCyhShopCattle;
import com.laowang.project.system.domain.WebUserShopCattle;
import com.laowang.project.system.service.MyCyhShopCattleService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.laowang.project.system.mapper.WebUserMapper;
import com.laowang.project.system.domain.WebUser;
import com.laowang.project.system.service.WebUserService;

import javax.validation.constraints.NotBlank;

/**
 * 网页端用户Service业务层处理
 *
 * @author laowang
 * @date 2023-10-16
 */
@Service
public class WebUserServiceImpl implements WebUserService {
    @Autowired
    private WebUserMapper webUserMapper;

    @Autowired
    private MyCyhShopCattleService myCyhShopCattleService;



    /**
     * 查询网页端用户
     *
     * @param userId 网页端用户ID
     * @return 网页端用户
     */
    @Override
    public WebUser selectWebUserById(Integer userId) {
        return webUserMapper.selectWebUserById(userId);
    }

    /**
     * 查询网页端用户列表
     *
     * @param webUser 网页端用户
     * @return 网页端用户
     */
    @Override
    public List<WebUser> selectWebUserList(WebUser webUser) {
        return webUserMapper.selectWebUserList(webUser);
    }

    /**
     * 新增网页端用户
     *
     * @param webUser 网页端用户
     * @return 结果
     */
    @Override
    public int insertWebUser(WebUser webUser) {
                                                                                                                                        return webUserMapper.insertWebUser(webUser);
    }

    /**
     * 修改网页端用户
     *
     * @param webUser 网页端用户
     * @return 结果
     */
    @Override
    public int updateWebUser(WebUser webUser) {
                                                                                                                                        return webUserMapper.updateWebUser(webUser);
    }

    /**
     * 批量删除网页端用户
     *
     * @param userIds 需要删除的网页端用户ID
     * @return 结果
     */
    @Override
    public int deleteWebUserByIds(Integer[] userIds) {
        return webUserMapper.deleteWebUserByIds(userIds);
    }

    /**
     * 删除网页端用户信息
     *
     * @param userId 网页端用户ID
     * @return 结果
     */
    @Override
    public int deleteWebUserById(Integer userId) {
        return webUserMapper.deleteWebUserById(userId);
    }

    @Override
    public void register(WebUser webUser) {
        @NotBlank(message = "手机号不能为空") String phone = webUser.getPhone();
        if (!MobileCheck.isMobile(phone)){
            throw new ClassCastException("手机号有误");
        }

        @NotBlank(message = "密码不能为空") String password = webUser.getPassword();
        @NotBlank(message = "重复密码不能为空") String password1 = webUser.getPassword1();
        if (!password.equals(password1)){
            throw new ClassCastException("两次输入的密码不一致");
        }

        Integer i = webUserMapper.selectByPhone(phone);
        if (i >0){
            throw new ClassCastException("该手机号已被注册");
        }


        webUserMapper.insertWebUser(webUser);



    }

    @Override
    public void login(WebUser webUser) {
        if (StringUtils.isEmpty(webUser.getPassword()) || StringUtils.isEmpty(webUser.getPhone())){
            throw new ClassCastException("手机号或密码为空");
        }

        Integer i = webUserMapper.selectByPhonePassword(webUser.getPhone(),webUser.getPassword());
        if (i == 0){
            throw new ClassCastException("手机号或密码错误");
        }
    }
}
